/**
 * 自动引入组件
 * */
import { kebabCase } from 'unplugin-vue-components'
import Components from 'unplugin-vue-components/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { getPath, getSep, setResolve } from '../utils'

export function configComponents() {
  return Components({
    dirs: ['src/components'],
    extensions: ['vue'],
    resolvers: [
      IconsResolver({
        prefix: false,
        customCollections: ['i'],
        enabledCollections: []
      }),
      {
        type: 'component',
        resolve: (name) => {
          const [prefix, folder] = kebabCase(name).split('-')
          // 命名导出组件
          if (prefix === 'eos') {
            return {
              name: name.slice(3),
              from: getPath(`${getSep('src')}/components`, { folder })
            }
          }
          // 默认导出组件
          if (prefix === 'eo') {
            return setResolve(name)
          }
          if (prefix === 'base') {
            return {
              name: name.slice(4),
              from: `${getSep('src')}/components/_base`
            }
          }
        }
      }
    ],
    directoryAsNamespace: true,
    dts: './config/unplugin/components.d.ts'
  })
}
